Skip to content

Conversation

@wjr-z
Copy link

@wjr-z wjr-z commented Oct 22, 2025

Optimize SetImpliedBits, avoid repeated updates multiple times
Details see #155808

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the llvm:mc Machine (object) code label Oct 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 22, 2025

@llvm/pr-subscribers-llvm-mc

Author: Weng Jinrui (wjr-z)

Changes

Optimize SetImpliedBits, avoid repeated updates multiple times


Full diff: https://github.com/llvm/llvm-project/pull/164583.diff

2 Files Affected:

  • (modified) llvm/include/llvm/TargetParser/SubtargetFeature.h (+13)
  • (modified) llvm/lib/MC/MCSubtargetInfo.cpp (+26-9)
diff --git a/llvm/include/llvm/TargetParser/SubtargetFeature.h b/llvm/include/llvm/TargetParser/SubtargetFeature.h
index a48b18745352a..a5bf1ef72fa68 100644
--- a/llvm/include/llvm/TargetParser/SubtargetFeature.h
+++ b/llvm/include/llvm/TargetParser/SubtargetFeature.h
@@ -20,6 +20,7 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/bit.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/MathExtras.h"
 #include <array>
@@ -151,6 +152,18 @@ class FeatureBitset {
     }
     return false;
   }
+
+  template <typename Func>
+  constexpr void forEachBit(Func func) const {
+    for (unsigned I = 0; I < MAX_SUBTARGET_WORDS; ++I) {
+      uint64_t Word = Bits[I];
+      while (Word) {
+        unsigned Bit = llvm::countr_zero(Word);
+        Word &= Word - 1;
+        func(I * 64 + Bit);
+      }
+    }
+  }
 };
 
 /// Class used to store the subtarget bits in the tables created by tablegen.
diff --git a/llvm/lib/MC/MCSubtargetInfo.cpp b/llvm/lib/MC/MCSubtargetInfo.cpp
index 89a08327dd259..77ea1bc5aa040 100644
--- a/llvm/lib/MC/MCSubtargetInfo.cpp
+++ b/llvm/lib/MC/MCSubtargetInfo.cpp
@@ -34,15 +34,32 @@ static const T *Find(StringRef S, ArrayRef<T> A) {
 }
 
 /// For each feature that is (transitively) implied by this feature, set it.
-static
-void SetImpliedBits(FeatureBitset &Bits, const FeatureBitset &Implies,
-                    ArrayRef<SubtargetFeatureKV> FeatureTable) {
-  // OR the Implies bits in outside the loop. This allows the Implies for CPUs
-  // which might imply features not in FeatureTable to use this.
-  Bits |= Implies;
-  for (const SubtargetFeatureKV &FE : FeatureTable)
-    if (Implies.test(FE.Value))
-      SetImpliedBits(Bits, FE.Implies.getAsBitset(), FeatureTable);
+static void SetImpliedBits(FeatureBitset &Bits, const FeatureBitset &Implies,
+                           ArrayRef<SubtargetFeatureKV> FeatureTable) {
+  std::array<uint16_t, MAX_SUBTARGET_FEATURES> featureMap;
+  FeatureBitset Mask;
+  uint16_t idx = 0;
+  for (const auto &FE : FeatureTable) {
+    Mask.set(FE.Value);
+    featureMap[FE.Value] = idx++;
+  }
+
+  FeatureBitset impl(Implies);
+  while (true) {
+    Bits |= impl;
+    auto newImplies = Mask & impl;
+    if (newImplies.none()) {
+      break;
+    }
+
+    Mask ^= newImplies;
+    impl = FeatureBitset();
+
+    newImplies.forEachBit([&](unsigned Bit) {
+      unsigned idx = featureMap[Bit];
+      impl |= FeatureTable[idx].Implies.getAsBitset();
+    });
+  }
 }
 
 /// For each feature that (transitively) implies this feature, clear it.

@wjr-z wjr-z closed this Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:mc Machine (object) code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants